package com.general.app.xtgl.action;

import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;

import com.general.app.xtgl.dao.entity.CompanyModel;
import com.general.app.xtgl.service.ICompanyService;
import com.general.common.action.BaseAction;
import com.general.common.model.QueryModel;
import com.general.common.security.entity.User;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 单位信息管理
 * @author Administrator
 *
 */
public class CompanyAction extends BaseAction implements ModelDriven<CompanyModel>{

	private static final long serialVersionUID = -5571279048868407604L;
	
	private CompanyModel model = new CompanyModel();
	private ICompanyService companyService;

	/**
	 * 单位信息管理列表
	 * @return
	 */
	public String companyList(){
		if (QUERY.equals(model.getDoType())) {
			QueryModel queryModel = model.getQueryModel();
			try {
				queryModel.setItems(companyService.getPagedList(model));
			} catch (Exception e) {
				e.printStackTrace();
			}
			getValueStack().set(DATA, queryModel);
			return DATA;
		}
		return "companyList";
	}	
	
	/**
	 * 单位增加面页
	 * @return
	 */
	public String companyAdd(){		
		return "companyAdd";
	}
	
	/**
	 * 保存单位信息
	 * @return
	 */
	public String saveCompanyAdd(){
		try {
			User user = getCurrentUser();
			model.setCompany_id(UUID.randomUUID().toString());
			model.setInsert_nm(user.getUserid());
			model.setUpdate_nm(user.getUserid());
			boolean result = companyService.insert(model);
			String key = result ? "I99001" : "I99002";
			getValueStack().set(DATA, getText(key));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}

	
	/**
	 * 单位修改面页
	 * @return
	 */
	public String companyMod(){
		
		ValueStack vs = getValueStack();
		String ids = getRequest().getParameter("ids");
		CompanyModel companyModel = companyService.getModel(ids);
		try {
			BeanUtils.copyProperties(model, companyModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		vs.set("model", model);		
		return "companyMod";
	}
	
	/**
	 * 保存单位修改面页
	 * @return
	 */
	public String saveCompanyMod(){
		try {

			User user = getCurrentUser();
			model.setUpdate_nm(user.getUserid());
			boolean result = companyService.update(model);
			String key = result ? "I99001" : "I99002";
			getValueStack().set(DATA, getText(key));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
	
	/**
	 * 删除单位信息
	 * @return
	 */
	public String companyDel(){

		String ids = getRequest().getParameter("ids");
		try {
			User user = getCurrentUser();
			String message = companyService.scDwxx(ids, user);
			getValueStack().set(DATA, message);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
	
	public ICompanyService getCompanyService() {
		return companyService;
	}
	public void setCompanyService(ICompanyService companyService) {
		this.companyService = companyService;
	}
	public void setModel(CompanyModel model) {
		this.model = model;
	}
	public CompanyModel getModel() {
		return model;
	}	
}
